package com.example.hospitalmanagement.activity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.hospitalmanagement.R
import com.example.hospitalmanagement.adapter.DrugPriceActivityAdapter
import com.example.hospitalmanagement.bean.DrugPriceData
import com.example.hospitalmanagement.viewmodel.DrugPriceViewModel
import com.xiasuhuei321.loadingdialog.view.LoadingDialog
import kotlinx.android.synthetic.main.activity_charge_item.*
import kotlinx.android.synthetic.main.activity_drug_price.*

/**
 * 药品价格Activity
 *
 * 位置:医院综合-价格公示-药品价格
 */
class DrugPriceActivity : AppCompatActivity() {
    private var pageNum = 1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_drug_price)
        initView()
    }

    /**
     * 正在加载中弹窗显示以及药品价格条目显示
     */
    private fun initView() {
        val drugPriceViewModel=ViewModelProviders.of(this).get(DrugPriceViewModel::class.java)
        val loadingDialog = LoadingDialog(this)
        val adapter = DrugPriceActivityAdapter(this)
        loadingDialog.setLoadingText("加载中")
            .show()
        val priceItem: RecyclerView = findViewById(R.id.drug_price_item)
        priceItem.layoutManager = LinearLayoutManager(this)
        val dataList: MutableList<DrugPriceData> = mutableListOf()
        val map: MutableMap<String, String> = mutableMapOf()
        map["pageSize"] = "10"
        map["pageNum"] = "$pageNum"
        drugPriceViewModel.getMedicine(map){
            it.forEach {data-> dataList.add(data) }
            adapter.notifyDataSetChanged()
        }
        adapter.dataList = dataList
        priceItem.adapter = adapter
        loadingDialog.close()
        smart_refresh_drug_price.setOnLoadMoreListener {
            smart_refresh_charge.finishLoadMore(5000, true, true)
            pageNum += 1
            map.clear()
            if ("" != charge_edittext.text.toString()) {
                map["searchName"] = charge_edittext.text.toString()
            }
            map["pageSize"] = "10"
            map["pageNum"] = "$pageNum"
            drugPriceViewModel.getMedicine(map) {
                dataList.addAll(it)
                adapter.notifyDataSetChanged()
            }
        }
        drug_price_btn.setOnClickListener {
            map.clear()
            if ("" != charge_edittext.text.toString()) {
                map["itemName"] = charge_edittext.text.toString()
            }
            map["pageSize"] = "10"
            map["pageNum"] = "1"
            drugPriceViewModel.getMedicine(map) {
                if (it.size > 0) {
                    dataList.addAll(it)
                    adapter.notifyDataSetChanged()
                }
            }
        }
    }
}